package org.scorpios.search.analyzer;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.analysis.Tokenizer;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.PathUtils;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AbstractTokenizerFactory;

import java.io.File;
import java.nio.file.Path;

/**
 * @author zhangxudong
 * @date 2022/11/12 20:20
 * @desc
 */
public class SearchTokenizerFactory extends AbstractTokenizerFactory {
    private final static Logger LOG = LogManager.getLogger(SearchTokenizerFactory.class);
    private final SearchTokenizer tokenizer;


    public SearchTokenizerFactory(IndexSettings indexSettings, Environment environment, String ignored, Settings settings) {
        super(indexSettings, settings, ignored);
        Path configPath = PathUtils.get(new File(SearchAnalyzer.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent()).toAbsolutePath().resolve("jieba_config.properties");
        LOG.info("开始加载配置文件:[{}]", configPath.toAbsolutePath());
        tokenizer = new SearchTokenizer();
    }

    @Override
    public Tokenizer create() {
        return tokenizer;
    }
}
